package top.lyc829.iot_android;

import android.app.Application;
import android.content.Context;

import com.blankj.utilcode.util.SPUtils;
import com.xuexiang.xhttp2.XHttpSDK;
import com.xuexiang.xpage.PageConfig;
import com.xuexiang.xpage.base.XPageActivity;

import java.util.UUID;

import top.lyc829.iot_android.http.interceptor.CustomDynamicInterceptor;
import top.lyc829.iot_android.http.interceptor.TokenInterceptor;

/**
 * @Author lyc
 * @create 2021-05-30 16:21
 */
public class MyApp extends Application {

    private static Context context;

    public static Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();

        initXPage();
        initXHttp2();

        initMQTT();

    }

    private void initXHttp2() {
        XHttpSDK.init(this);   //初始化网络请求框架，必须首先执行
        //XHttpSDK.debug("XHttp");  //需要调试的时候执行
        XHttpSDK.debug(new TokenInterceptor("XHttp"));
        XHttpSDK.setSuccessCode(200);
        XHttpSDK.setBaseUrl("http://10.0.2.2:8080");  //设置网络请求的基础地址

        XHttpSDK.addInterceptor(new CustomDynamicInterceptor()); //设置动态参数添加拦截器

    }

    private void initXPage() {
        PageConfig.getInstance()
//      //页面注册,默认不设置的话使用的就是自动注册
//      .setPageConfiguration(new AutoPageConfiguration())
                .debug("PageLog")       //开启调试
                .setContainActivityClazz(XPageActivity.class) //设置默认的容器Activity
                .init(this);            //初始化页面配置
    }

    private void initMQTT() {

        if (SPUtils.getInstance().getString("client_id", "-1").equals("-1")) {
            SPUtils.getInstance().put("client_id", UUID.randomUUID().toString());
        }
    }


}
